 package com.lbs.core.shared.parsing;
 
 import com.lbs.core.shared.XmlRawInterface;
import com.lbs.core.shared.tables.AbstractData;
import java.util.ArrayList;
import java.util.List;
 
 public class LbsCondition
   implements LbsTokenInterface
 {
   private LbsBasicToken leftToken;
   private LbsBasicToken rigthToken;
   private Boolean result;
   private String operator;
 
   public LbsCondition(String string, XmlRawInterface xml, AbstractData data)
     throws LbsParserException
   {
     string = string.trim();
     if (string.contains("==")) {
       operator = "==";
       String[] elements = string.split("==");
       if (elements.length > 2)
         throw new LbsParserException("Error: Condicion invalida: " + string);
       leftToken = new LbsBasicToken(elements[0].trim(), xml, data);
       rigthToken = new LbsBasicToken(elements[1].trim(), xml, data);
     } else if (string.contains("!=")) {
       operator = "!=";
       String[] elements = string.split("!=");
       if (elements.length > 2)
         throw new LbsParserException("Error: Condicion invalida: " + string);
       leftToken = new LbsBasicToken(elements[0].trim(), xml, data);
       rigthToken = new LbsBasicToken(elements[1].trim(), xml, data);
     } else if (string.contains("<=")) {
       operator = "<=";
       String[] elements = string.split("<=");
       if (elements.length > 2)
         throw new LbsParserException("Error: Condicion invalida: " + string);
       leftToken = new LbsBasicToken(elements[0].trim(), xml, data);
       rigthToken = new LbsBasicToken(elements[1].trim(), xml, data);
     } else if (string.contains(">=")) {
       operator = ">=";
       String[] elements = string.split(">=");
       if (elements.length > 2)
         throw new LbsParserException("Error: Condicion invalida: " + string);
       leftToken = new LbsBasicToken(elements[0].trim(), xml, data);
       rigthToken = new LbsBasicToken(elements[1].trim(), xml, data);
     } else if (string.contains("<")) {
       operator = "<";
       String[] elements = string.split("<");
       if (elements.length > 2)
         throw new LbsParserException("Error: Condicion invalida: " + string);
       leftToken = new LbsBasicToken(elements[0].trim(), xml, data);
       rigthToken = new LbsBasicToken(elements[1].trim(), xml, data);
     } else if (string.contains(">")) {
       operator = ">";
       String[] elements = string.split(">");
       if (elements.length > 2)
         throw new LbsParserException("Error: Condicion invalida: " + string);
       leftToken = new LbsBasicToken(elements[0].trim(), xml, data);
       rigthToken = new LbsBasicToken(elements[1].trim(), xml, data);
     } else {
       throw new LbsParserException("Error: Operador no programado.");
     }
   }
   
   /**
    * Evalua la expresion y regresa el resultado
    * 
    * @return
    */
   public Boolean getResult() { 
	   	Object l =  leftToken.getVal(); 
		Object r =  rigthToken.getVal();
		//Cadena vacia es lo mismo que null
		if (l instanceof String && l.toString().isEmpty())
			l=null;
		if (r instanceof String && r.toString().isEmpty())
			r=null;
		
	 //Verificaciones
	 if (operator.equals("==")) {
		 if (l instanceof String && r instanceof String){
	       if (l.equals(r))
	         result = Boolean.valueOf(true);
	       else
	         result = Boolean.valueOf(false);
		 }else{			 
	       if (l == r)
	         result = Boolean.valueOf(true);
	       else
	         result = Boolean.valueOf(false);
		 }
     } else if (operator.equals("!=")) {
       if (l != r)
         result = Boolean.valueOf(true);
       else
         result = Boolean.valueOf(false);
     } else if (operator.equals("<=")) {
       if (((Double)l).doubleValue() <= ((Double)r).doubleValue())
         result = Boolean.valueOf(true);
       else
         result = Boolean.valueOf(false);
     } else if (operator.equals(">=")) {
       if (((Double)l).doubleValue() >= ((Double)r).doubleValue())
         result = Boolean.valueOf(true);
       else
         result = Boolean.valueOf(false);
     } else if (operator.equals("<")) {
       if (((Double)l).doubleValue() < ((Double)r).doubleValue())
         result = Boolean.valueOf(true);
       else
         result = Boolean.valueOf(false);
     } else if (operator.equals(">")) {
       if (((Double)l).doubleValue() > ((Double)r).doubleValue())
         result = Boolean.valueOf(true);
       else {
         result = Boolean.valueOf(false);
       }
     }
     return result;
   }
 
   @Override
public Object getVal()
   {
     return getResult();
   }
 
   @Override
public List<String> getFields()
   {
     List<String> result = new ArrayList<String>();
     result.addAll(leftToken.getFields());
     result.addAll(rigthToken.getFields());
     return result;
   }
 }

